/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmPublisherManager.java
 *
 * Created on 11-03-2011, 00:18:20
 */

package GUI;

import interact.DataInteraction;
import interact.ValidateData;
import interact.connect;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author UnderGround
 */
public class frmPublisherManager extends javax.swing.JPanel {

    /** Creates new form frmPublisherManager */
    public frmPublisherManager() {
        initComponents();
        loadDataTable();
    }
    public static void loadDataTable(){
        connect.loadDataTable3("getPublisherToTable", tblPublisher);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPublisher = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
        jPanel1 = new javax.swing.JPanel();
        btnUpdate = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnDel = new javax.swing.JButton();
        txtPublisher = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(715, 490));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jDesktopPane1.setBackground(new java.awt.Color(102, 102, 102));
        jDesktopPane1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(178, 210, 240), null, null));
        jDesktopPane1.setPreferredSize(new java.awt.Dimension(715, 490));

        tblPublisher.setBackground(new java.awt.Color(255, 255, 204));
        tblPublisher.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblPublisher.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblPublisher);

        jScrollPane1.setBounds(0, 0, 720, 370);
        jDesktopPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPanel1.setBackground(new java.awt.Color(153, 153, 153));

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/up.png"))); // NOI18N
        btnUpdate.setText("update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon_publish.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnDel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/del.png"))); // NOI18N
        btnDel.setText("delete");
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });

        jLabel1.setText("Publisher name");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(178, 178, 178)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnDel, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtPublisher, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(226, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPublisher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDel)
                    .addComponent(btnUpdate)
                    .addComponent(btnAdd))
                .addContainerGap())
        );

        jPanel1.setBounds(0, 370, 720, 140);
        jDesktopPane1.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        add(jDesktopPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 510));
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        publisherName = txtPublisher.getText();
        if (!ValidateData.checkEmpty(publisherName, txtPublisher)) {
            ValidateData.showMessage("<html><center>publishing house invalid.<br> please try again</center></html>");
            return;
        }else if(!ValidateData.checkDuplicateData(publisherName, "checkDuplicate_publisher", "count")){
            ValidateData.showMessage("duplicate publisher");
            return;
        }
        else {
            vt = new Vector();
            vt.add(publisherName);
            DataInteraction.updateData("addPublisher", vt);
            loadDataTable();
            ValidateData.showMessage("add publishing house successful");
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        rowSelected = tblPublisher.getSelectedRow();
        publisherName = txtPublisher.getText().trim();
        System.out.println(ValidateData.checkDuplicateData(publisherName, "checkDuplicate_publisher", "count"));
        if(rowSelected == -1){
            ValidateData.showMessage("Please choose publishing house to edit");
            return;
        }if(!ValidateData.checkEmpty(publisherName, txtPublisher)){
            ValidateData.showMessage("publishing house invalid. please try again");
            return;
        }else if(!ValidateData.checkDuplicateData(publisherName, "checkDuplicate_publisher", "count")){
            ValidateData.showMessage("duplicate publisher");
            return;
        }
        
        else{
            id =(String) tblPublisher.getValueAt(rowSelected, 0);
        vt = new Vector();
        vt.add(id);
        vt.add(publisherName);
        DataInteraction.updateData("updatePublisher", vt);
        loadDataTable();
        ValidateData.showMessage("update successful");
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
        rowSelected = tblPublisher.getSelectedRow();
        if (rowSelected == -1) {
            ValidateData.showMessage("Please choose a book to delete");
            return;
        }
        id = (String) tblPublisher.getValueAt(rowSelected, 0);
         if (ValidateData.check_Status_To_Del("check_del_publishingHouse", id)) {
            ValidateData.showMessage("You can't delete this publishing house");
            return;
        } else {
            DataInteraction.deleteRecord("delPublisher", id);
            loadDataTable();
        }
    }//GEN-LAST:event_btnDelActionPerformed

    private Vector vt;
    private int rowSelected;
    private String id,publisherName;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDel;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTable tblPublisher;
    private javax.swing.JTextField txtPublisher;
    // End of variables declaration//GEN-END:variables

}
